//
//  ChatManager.m
//  PigMarket
//
//  Created by mac on 2019/6/4.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//
#import "ChatManager.h"
#import <TIMComm.h>
#import <TIMFriendshipManager.h>

#define kIMAppId 1400194609
#define kIMAccountType  @"36862"

static ChatManager *instance = nil;
@implementation ChatManager

+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[ChatManager alloc] init];
    });
    return instance;
}

- (void)loginIMWithUserName:(NSString *)userName userSig:(nonnull NSString *)userSig succ:(nonnull void (^)(void))succ fail:(nonnull void (^)(int, NSString * _Nonnull))fail{


    if (kIMAppId == 0) {
        [[AlertAction sharedInstance] showAlertWithTitle:@"提示" message:@"IM配置问题" confirmBtnTitle:nil confirmBlock:nil cancelBlock:nil];
        return;
    } else {

        [[TUIKit sharedInstance] setupWithAppId:kIMAppId];
        [self IMUIDefaultConfig];
    }
    /*
     *  登录参数
     *  1.identifier: 用户名
     *  2.userSig: 鉴权 Token,由后台生成
     */
    TIMLoginParam *param = [[TIMLoginParam alloc] init];
    param.identifier = userName;
    param.userSig = userSig;
    [[TIMManager sharedInstance] login:param succ:^{
        succ();
    } fail:^(int code, NSString *msg) {
        fail(code,msg);
    }];
}

//配置
- (void)IMUIDefaultConfig {
    
    //禁止打印log日志
    TIMSdkConfig *config = [[TIMSdkConfig alloc] init];
    config.logLevel = TIM_LOG_NONE;
    config.disableLogPrint = YES;
    
    //设置头像类型
    [TUIKitConfig defaultConfig].avatarType = TAvatarTypeRounded;
    
    //默认头像
    [TUIKitConfig defaultConfig].defaultAvatarImage = [UIImage imageNamed:@"Information_headImage"];
    
    //默认群组头像
    [TUIKitConfig defaultConfig].defaultGroupAvatarImage = [UIImage imageNamed:@"Information_headImage"];
    
    //设置好友申请需要验证
    [[TIMFriendshipManager sharedInstance] modifySelfProfile:@{TIMProfileTypeKey_AllowType:[NSNumber numberWithInteger:TIM_FRIEND_NEED_CONFIRM]} succ:^{
       
    } fail:^(int code, NSString *msg) {
        
    }];
}
@end
